/*
 * Copyright (c) 2021 Talkweb Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "nfc_ndef.h"

/**
 * @brief NDEF包制作函数
 * @param ndef 传入NDEF相关参数
 * @param mem 保存根据NDEF参数制作的NDEF数据
 * @return 返回TRUE表示NDEF数据制作成功
 */
BOOL NdefPackMake(NdefFrame *ndef, MemFrame *mem)
{
    if (ndef->payloadLen > NDEF_RECORD_SIZE)
    {
        printf("payloadLen is too long, payloadLen = %u, maxLen = %u\r\n", ndef->payloadLen, NDEF_RECORD_SIZE);
        return FALSE;
    }

    switch (ndef->packtype)
    {
    case START_PACK:
        mem->data[0] = 0x91;
        break;

    case MIDDLE_PACK:
        mem->data[0] = 0x11;
        break;

    case END_PACK:
        mem->data[0] = 0x51;
        break;

    case START_END_PACK:
        mem->data[0] = 0xD1;
        break;

    default:
        printf("not support packtype \r\n");
        return FALSE;
    }

    mem->data[1] = 0x01;
    switch (ndef->dataType)
    {
    case RTD_UTF8:
        ndef->payloadLen += 3;
        break;

    case RTD_URI:
        ndef->payloadLen += 1;
        break;

    default:
        printf("not support dataType \r\n");
        return FALSE;
    }
    mem->data[2] = ndef->payloadLen;
    mem->data[3] = ndef->dataType;
    mem->len = ndef->payloadLen + 4;

    if (ndef->dataType == RTD_UTF8)
    {
        mem->data[4] = 2;
        mem->data[5] = 'z';
        mem->data[6] = 'h';
        memcpy_s(&mem->data[7], ndef->payloadLen, ndef->payload, ndef->payloadLen);
    }
    else
    {
        mem->data[4] = ndef->uriVal;
        memcpy_s(&mem->data[5], ndef->payloadLen, ndef->payload, ndef->payloadLen);
    }

    return TRUE;
}

/**
 * @brief NDEF包解析函数
 * @param ndef 保存解析后的NDEF相关参数
 * @param mem 被解析的NDEF数据
 * @return 返回TRUE表示NDEF数据解析成功
 */
BOOL NdefPackParsing(NdefFrame *ndef, unsigned char *mem)
{
    NdefHead head; 

    memcpy_s(&head, 1, &mem[0], 1);
    if(head.IL == 1)
    {
        printf("NdefPackParsing() err: dont support ndef ID type\r\n");
        return FALSE;
    }

    if(head.SR != 1)
    {
        printf("NdefPackParsing() err: dont support ndef SR = 0 type\r\n");
        return FALSE;
    }

    if(head.TNF != TNF_INSIDE_RTD)
    {
        printf("NdefPackParsing() err: ndef TNF != TNF_INSIDE_RTD type\r\n");
        return FALSE;
    }

    ndef->payloadLen = mem[2];
    if(ndef->payloadLen > NDEF_RECORD_SIZE)
    {
        printf("NdefPackParsing() err: payloadLen = %u, NDEF_RECORD_SIZE = %u\r\n",ndef->payloadLen,NDEF_RECORD_SIZE);
        return FALSE;       
    }

    ndef->dataType = mem[3]; 
    if((ndef->dataType != RTD_UTF8)&&(ndef->dataType != RTD_URI))
    {
        printf("NdefPackParsing() err: dataType is invalid\r\n");
        return FALSE;   
    }

    unsigned char type = (head.MB<<2)|(head.ME<<1)|head.CF;
    switch(type)
    {
        case 0: ndef->packtype = MIDDLE_PACK; break;
        case 2: ndef->packtype = END_PACK; break;
        case 4: ndef->packtype = START_PACK; break;
        case 6: ndef->packtype = START_END_PACK; break;
        default: printf("NdefPackParsing() err: packtype is invalid!\r\n"); return FALSE;       
    }

    if(ndef->dataType == RTD_UTF8)
    {
        ndef->payloadLen -= 3;
        memcpy_s(ndef->payload,NDEF_RECORD_SIZE,&mem[4+3], ndef->payloadLen);
    }
    else
    {
        ndef->uriVal = mem[4];
        ndef->payloadLen -= 1;
        memcpy_s(ndef->payload,NDEF_RECORD_SIZE,&mem[4+1], ndef->payloadLen);
    }

    return TRUE;   
}
